﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Documents;
using Prism.Regions;
using Quartz;

namespace ZYing.QPanel.ViewModels
{
    public class HistoryDetailViewModel : NavigationViewModelBase
    {
        private readonly IScheduler _scheduler;
        public HistoryDetailViewModel(IScheduler scheduler)
        {
            this._scheduler = scheduler ?? throw new ArgumentNullException(nameof(scheduler));
            Run(this.CancellationTokenSource.Token);
        }
        private string _id;

        public string Id
        {
            get { return _id; }
            set { SetProperty(ref _id, value); }
        }

        public override bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return base.IsNavigationTarget(navigationContext);
        }
        public override async void OnNavigatedTo(NavigationContext navigationContext)
        {
            base.OnNavigatedTo(navigationContext);
            if (navigationContext.Parameters.TryGetValue<string>("id", out var id))
            {
                Id = id;
            }
            else
            {
                Id = string.Empty;
            }
            Entry = null;
            Logs.Clear();
            await Reload();
        }
        private ExecutionHistoryEntry _entry;

        public ExecutionHistoryEntry Entry
        {
            get { return _entry; }
            set { SetProperty(ref _entry, value); }
        }

        public ObservableCollection<ExecutionLogEntry> Logs { get; } = new ObservableCollection<ExecutionLogEntry>();
        private async void Run(CancellationToken cancellationToken)
        {
            while (true)
            {
                if (cancellationToken.IsCancellationRequested) break;
                if (this.IsActive)
                {
                    try
                    {
                        await Reload();
                    }
                    catch (Exception e)
                    {
                        Trace.WriteLine(e);
                    }
                }
                await Task.Delay(1000);
            }
        }
        private async Task Reload()
        {
            if (string.IsNullOrWhiteSpace(Id)) return;
            if (!this._scheduler.Context.TryGetExecutionHistoryStore(out var store)) return;
            var entry = await store.Find(Id);
            if (entry == null) return;
            this.Entry = entry;
            this.RaisePropertyChanged(nameof(Entry));
            var logs = await store.GetLogs(Id);
            if (logs.Count > this.Logs.Count)
            {
                for (int i = this.Logs.Count; i < logs.Count; i++)
                {
                    var log = logs[i];
                    this.Logs.Insert(0, log);
                }
            }
        }
    }
}
